package com.endbd.systemmanagement.controller.user;

import com.endbd.feignapi.model.system.User;
import com.endbd.systemmanagement.model.*;
import com.endbd.systemmanagement.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/system")
@RestController
public class UserController {

  @Autowired
  private UserService userService;

  @GetMapping("/user/userInfo/{uid}")
  public ResultBody queryUsersByPage(@PathVariable("uid") Integer uId){
    UserCondition userCondition = new UserCondition();
    userCondition.setUid(uId);
    PageUsers<UserInfo> user = userService.queryUserByCondition(userCondition,1,1);
    ResultBody body = new ResultBody();
    if(user.getList().isEmpty()){
      body.setCode(500);
      body.setMessage("获取用户信息失败");
      body.setData(null);
    }else{
      body.setCode(200);
      body.setMessage("success");
      body.setData(user.getList().get(0));
    }
    return body;
  }

  @PostMapping("/user/queryByCondition/{page}/{size}")
  public ResultBody queryUserByCondition(@RequestBody UserCondition userCondition, @PathVariable int page,@PathVariable int size){
    ResultBody resultBody = new ResultBody();
    PageUsers<UserInfo> pageUsers=userService.queryUserByCondition(userCondition,page,size);
    if(pageUsers.getList().isEmpty()){
      resultBody.setCode(500);
      resultBody.setMessage("未查找到相应数据");
      resultBody.setData(null);
    }else{
      resultBody.setCode(200);
      resultBody.setMessage("success");
      resultBody.setData(pageUsers);
    }
    return resultBody;
  }

  @PostMapping("/user/create")
  public ResultBody createUser(@RequestBody User user){
    ResultBody resultBody = new ResultBody();
    int b=userService.create(user);
    if(b==1){
      resultBody.setCode(200);
      resultBody.setMessage("添加新用户==>"+user.getUId());
    }else if(b==2){
      resultBody.setCode(501);
      resultBody.setMessage("uid已存在");
    }else{
      resultBody.setCode(500);
      resultBody.setMessage("error");
    }
    return resultBody;
  }

  @PostMapping("/user/update")
  public ResultBody updateUser(@RequestBody User user){
    ResultBody resultBody = new ResultBody();
    boolean flag=userService.updateUser(user);
    if(flag) {
      resultBody.setCode(200);
      resultBody.setMessage("修改用户=>"+user.getUId());
    }
    else resultBody.setCode(500);
    return resultBody;
  }

}
